package pers.flowerinsnow.minecraft.forge.shiftability.listener

import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import pers.flowerinsnow.minecraft.forge.shiftability.manager.ISAConfigManager

class TickListener {
    @SubscribeEvent
    fun onTick(e : TickEvent.ClientTickEvent) {
        if (Minecraft.getMinecraft().theWorld == null) return
        if (!ISAConfigManager.ConfigEntry.GENERAL_ENABLE.get().boolean) {
            next = -1L
            return
        }
        if (next == -2L) {
            Minecraft.getMinecraft().thePlayer.isSneaking = false
            next = System.currentTimeMillis() + ISAConfigManager.ConfigEntry.GENERAL_PERIOD.get().int
            return
        }
        if (next != -1L && next <= System.currentTimeMillis()) {
            Minecraft.getMinecraft().thePlayer.isSneaking = true
            next = -2L
        }
    }
    companion object {
        var next = -1L
            get() {
                synchronized(field) {
                    return field
                }
            }
            set(value) {
                synchronized(field) {
                    field = value
                }
            }
    }
}